

package com.hazelcast.map.impl.query;

import com.hazelcast.internal.util.collection.PartitionIdSet;

import static com.hazelcast.internal.util.Preconditions.checkNotNull;
import static com.hazelcast.map.impl.query.Target.TargetMode.PARTITION_OWNER;

/**
 * Target for a query.
 * <p>
 * Possible options:<ul>
 * <li>all nodes
 * <li>local node only
 * <li>given partition set
 * </ul>
 */
public final class Target {

    public static final Target ALL_NODES = new Target(TargetMode.ALL_NODES, null);
    public static final Target LOCAL_NODE = new Target(TargetMode.LOCAL_NODE, null);

    private final TargetMode mode;
    private final PartitionIdSet partitions;

    private Target(TargetMode mode, PartitionIdSet partitions) {
        this.mode = checkNotNull(mode);
        this.partitions = partitions;
        if (mode == PARTITION_OWNER ^ partitions != null) {
            throw new IllegalArgumentException("partitions must be used only with PARTITION_OWNER mode and not otherwise");
        }
    }

    public static Target createPartitionTarget(PartitionIdSet partitions) {
        return new Target(TargetMode.PARTITION_OWNER, partitions);
    }

    public TargetMode mode() {
        return mode;
    }

    public PartitionIdSet partitions() {
        return partitions;
    }

    @Override
    public String toString() {
        return "Target{" + "mode=" + mode + ", partitionsSize=" + (partitions == null ? 0 : partitions.size()) + '}';
    }

    public enum TargetMode {
        LOCAL_NODE, ALL_NODES, PARTITION_OWNER
    }
}
